package Chapter01.Homework;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
//4:用 PreparedStatement 实现删除功能（表随意）
public class Work04 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url="jdbc:mysql://localhost:3306/kgc76?useSSL=false";
        Connection connection=null;
        PreparedStatement ps=null;
        try {
            connection= DriverManager.getConnection(url,"root","123456");
            System.out.println(connection.getClass().getName());
            System.out.println("连接已经建立");
            Scanner input=new Scanner(System.in);
            System.out.println("请输入学号：");
            int stuno=input.nextInt();
            String sql="delete from s_student where stuno=?";
            System.out.println(sql);
            ps=connection.prepareStatement(sql);
            //!!给占位符赋值
            ps.setInt(1,stuno);
            int zhi=ps.executeUpdate();
            System.out.printf("%d行被修改了",zhi);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                ps.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
